import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import scipy.cluster.hierarchy as sch
from scipy.cluster.hierarchy import dendrogram, linkage, set_link_color_palette
from sklearn import preprocessing

df = pd.read_csv("F:/成信大/多元统计分析/test3-2.csv")
data = df.columns[1: 9]
u = df[data]
x_scaled = preprocessing.scale(u)
# print(u.columns)
# print(u.describe())


'''
热力图
'''
# sns.set()
# cor = df.corr()
# sns.heatmap(cor, square=True)
# plt.show()

'''
聚类结果图
'''
ZT = linkage(np.transpose(x_scaled), 'ward')
dendrogram(ZT, labels=u.columns, orientation='right')
my_palette = plt.cm.get_cmap("Accent", 4)
# plt.show()

x1 = df['Population']
x2 = df['Income']
x3 = df['Illiteracy']
x4 = df['Life Exp']
x5 = df['Murder']
x6 = df['HS Grad']
x7 = df['Frost']
x8 = df['Area']
disMat = sch.distance.pdist(
    X=df[['Population', 'Income', 'Illiteracy', 'Life Exp', 'Murder', 'HS Grad', 'Frost', 'Area']], metric='euclidean')
Z = sch.linkage(disMat, method='ward')
p = sch.dendrogram(Z, labels=df.PLACE.tolist())
# 49
print(p['color_list'])
# plt.savefig('聚类分析.png')
